<template>
    <div class="app-container">
        <div style="text-align: center;margin-bottom: 30px">
            巡检签到信息更新
        </div>
        <el-form :model="signTaskFrom"  ref="signTaskFrom" :rules="signTaskFromRules" label-width="100px">
            <el-row :gutter="10">
                <el-col :xs="24" :sm="24" :md="24" :lg="5" :xl="5">
                    <el-form-item label="签到图片 " prop="signPic">
                        <el-upload
                                class="avatar-uploader"
                                :action="uploadImg"
                                :headers="headers"
                                :data="{type:'pic'}"
                                :show-file-list="false"
                                :on-success="handleAvatarSuccess"
                                :before-upload="beforeAvatarUpload">
                            <img v-if="signTaskFrom.signPic&& imgFlag == false" :src="signTaskFrom.signPic" class="avatar">
                            <el-progress v-if="imgFlag == true" type="circle" :percentage="imgUploadPercent" style="margin-top:30px;"></el-progress>
                            <i v-else-if="!signTaskFrom.signPic&& imgFlag == false" class="el-icon-plus avatar-uploader-icon"></i>
                        </el-upload>
                    </el-form-item>
                </el-col>
                <el-col :xs="24" :sm="24" :md="24" :lg="18" :xl="18">
                    <el-col :xs="24" :sm="24" :md="12" :lg="12" :xl="8">
                        <el-form-item label="所属主体" prop="enterpriseId">
                            <el-select v-model="signTaskFrom.enterpriseId" placeholder="请选择主体" style="width: 100%" clearable @clear="clearInput()">
                                <el-option v-for="(item,index) in enterpriseDominantList" :key="index"  :value="item.ucId" :label="item.name"></el-option>
                            </el-select>
                        </el-form-item>
                    </el-col>
                    <el-col :xs="24" :sm="24" :md="12" :lg="12" :xl="8">
                        <el-form-item label="任务名称" prop="taskId">
                            <el-select v-model="signTaskFrom.taskId" placeholder="请选择任务" style="width: 100%"  clearable @clear="clearInput1()">
                                <el-option v-for="(item,index) in taskList" :key="index"  :value="item.ucId" :label="item.name"></el-option>
                            </el-select>
                        </el-form-item>
                    </el-col>
                    <el-col :xs="24" :sm="24" :md="12" :lg="12" :xl="8">
                        <el-form-item label="设备名称" prop="equipmentId">
                            <el-select v-model="signTaskFrom.equipmentId" placeholder="请选择设备" style="width: 100%" clearable @clear="clearInput2()" >
                                <el-option v-for="(item,index) in equipmentList" :key="index"  :value="item.ucId" :label="item.name"></el-option>
                            </el-select>
                        </el-form-item>
                    </el-col>
                    <el-col :xs="24" :sm="24" :md="12" :lg="12" :xl="8">
                        <el-form-item label="签到人员名称" prop="signUser">
                            <el-select v-model="signTaskFrom.signUser" placeholder="请选择设备" style="width: 100%" clearable  >
                                <el-option v-for="(item,index) in userList" :key="index"  :value="item.ucId" :label="item.name"></el-option>
                            </el-select>
                        </el-form-item>
                    </el-col>
                    <el-col :xs="24" :sm="24" :md="12" :lg="12" :xl="8">
                        <el-form-item label="签到时间" prop="signTime">
                            <el-date-picker
                                    v-model="signTaskFrom.signTime"
                                    type="datetime"
                                    placeholder="选择日期"
                                    style="width: 100%">
                            </el-date-picker>
                        </el-form-item>
                    </el-col>
                    <el-col :xs="24" :sm="24" :md="12" :lg="12" :xl="8">
                        <el-form-item label="反馈类型 " prop="feedbackStatus">
                            <el-select v-model="signTaskFrom.feedbackStatus" placeholder="反馈类型" style="width: 100%">
                                <el-option :value="0" label="正常"></el-option>
                                <el-option :value="1" label="异常"></el-option>
                            </el-select>
                        </el-form-item>
                    </el-col>
                    <el-col :xs="24" :sm="24" :md="12" :lg="12" :xl="8">
                        <el-form-item label="反馈内容" prop="feedback">
                            <el-input type="text" v-model="signTaskFrom.feedback" placeholder="请输入企业介绍" clearable></el-input>
                        </el-form-item>
                    </el-col>
                </el-col>
            </el-row>
        </el-form>
        <div slot="footer" style="text-align: center" class="dialog-footer">
            <el-button @click="cancelsignTaskFrom">取 消</el-button>
            <el-button :loading="addLoading"  type="primary" @click="updatesignTaskFrom">修 改</el-button>
        </div>
    </div>
</template>

<script>
import {uploadImg} from "@/api/common";
import {addInAbnormal, getInAbnormalVo, updateInAbnormal} from "@/api/abnormal";
import {updateInspectionTaskSign,getInspectionTaskSignVo} from "@/api/inspectionTaskSign";
import {getInEnterpriseList, getInUserList} from "@/api/enterprise";
import {getToken} from "@/utils/auth";
import {getInTaskList} from "@/api/taskinfo";
import {getEquipmentList} from "@/api/equipment";
import {getInEnterpriseDominantList} from "@/api/enterpriseDominant";

export default {
    name: "InsignTaskFromEdit",
    data() {
        return {
            uploadImg,
            signTaskFrom: {

            },
            signTaskFromRules: {
                name: [{required: true, message: '异常名称不能为空', trigger: 'blur'}],
                remark: [{required: true, message: '异常详情不能为空', trigger: 'blur'}],
                taskId: [{required: true, message: '申报人不能为空', trigger: 'blur'}],
                enterpriseId: [{required: true, message: '企业不能为空', trigger: 'blur'}],
            },
            addLoading: false,
            imgFlag: false,
            imgUploadPercent: 0,
            enterpriseDominantList:[],
            userList:[],
            taskList:[],
            equipmentList:[]

        }
    },
    created() {
        this.getInEnterpriseDominantList()
        this.getInUserList()
        this.getInTaskList()
        this.getEquipmentList()
        this.getInspectionTaskSignVo (this.$route.params.id)

    },
    computed: {
        headers() {
            return {
                'Authorization': getToken()
            }
        },
    },
    methods: {
        getInspectionTaskSignVo(val) {
            getInspectionTaskSignVo({id: val}).then(res => {
                console.log(res)
                this.signTaskFrom = res.data
                console.log(this.signTaskFrom)
            }).catch(res => {

            })
        },
        clearInput(){
            //清除企业下拉框
            this.searchForm.enterpriseId = null
            this.handleSearch()
        },
        cancelsignTaskFrom() {
            let view = {
                fullPath: this.$route.path,
                path: this.$route.path,
                name: this.$route.name,
                meta: {...this.$route.meta}
            }
            this.$store.dispatch('tagsView/delView', view).then(({visitedViews}) => {
                if (view.path == this.$route.path) {
                    const latestView = visitedViews.slice(-1)[0]
                    console.log(latestView)
                    if (latestView) {
                        this.$router.push(latestView.fullPath)
                    } else {
                        // now the default is to redirect to the home page if there is no tags-view,
                        // you can adjust it according to your needs.
                        if (view.name === 'Dashboard') {
                            // to reload home page
                            this.$router.replace({path: '/redirect' + view.fullPath})
                        } else {
                            this.$router.push('/')
                        }
                    }
                }
            })
        },
        updatesignTaskFrom() {
            this.addLoading = true
            this.$refs['signTaskFrom'].validate(valid => {
                if (valid) {
                    updateInspectionTaskSign(this.signTaskFrom).then(res => {
                        this.addLoading = false
                        this.$notify.success({
                            title: '成功',
                            message: "更新巡检签到成功"
                        })
                        this.cancelsignTaskFrom()
                    }).catch(res => {
                        this.addLoading = false
                        this.$notify.error({
                            title: '失败',
                            message: res.msg
                        })
                    })
                } else {
                    this.addLoading = false
                    console.log("表单验证不通过")
                }
            })
        },
        handleAvatarSuccess(res, file) {
            //图片上传成功
            console.log(res)
            this.imgFlag = false
            this.signTaskFrom.signPic = res.data;
        },
        uploadImgProcess(event, file, fileList){
            this.imgFlag = true;
            this.imgUploadPercent = file.percentage.toFixed(0);
        },
        beforeAvatarUpload(file) {
            const isJPG = file.type === 'image/jpeg'||file.type === 'image/png';
            const isLt2M = file.size / 1024 / 1024 < 2;
            if (!isJPG) {
                this.$message.error('上传头像图片只能是 JPG、png格式!');
            }
            if (!isLt2M) {
                this.$message.error('上传头像图片大小不能超过 2MB!');
            }
            return isJPG && isLt2M;
        },
        getInEnterpriseDominantList(){
            getInEnterpriseDominantList({deleted:false}).then(res=>{
                this.enterpriseDominantList = res.data
            }).catch(error=>{

            })
        },
        getInUserList(){
            getInUserList({deleted:false}).then(res=>{
                this.userList = res.data
            }).catch(error=>{
                alert("获取申报人列表失败")
            })
        },
        getInTaskList(){
            getInTaskList({deleted:false}).then(res=>{
                this.taskList = res.data
            }).catch(error=>{

            })
        },
        getEquipmentList(){
            getEquipmentList({deleted:false}).then(res=>{
                this.equipmentList = res.data
            }).catch(error=>{

            })
        },
    }
}

</script>

<style>
.avatar-uploader .el-upload {
    border: 1px dashed #d9d9d9;
    border-radius: 6px;
    cursor: pointer;
    position: relative;
    overflow: hidden;
}
.avatar-uploader .el-upload:hover {
    border-color: #409EFF;
}
.avatar-uploader-icon {
    font-size: 28px;
    color: #8c939d;
    width: 149px;
    height: 149px;
    line-height: 149px;
    text-align: center;
}
.avatar {
    width: 149px;
    height: 149px;
    display: block;
}
</style>


